
import QtQuick 1.0

import CustomItems 1.0
import SocketItems 1.0

Rectangle {
    id: main
    width: 360; height: 640
    color: "black"

    Image {
        source:"digia_logo.png"
        smooth: true
        x: 8
        y: 0
    }

    Rectangle {
        x: messageId.x - 10
        y: messageId.y - 10
        width: messageId.width + 20
        height: messageId.height + 20
        color: "white"
        smooth: true
        radius: 4
    }

    Rectangle {
        id: messageId
        property string message
        anchors.fill: camera
        anchors.margins: -10
        color: "red"
        smooth: true
        radius: 4
        Text {
            text: parent.message
            anchors.centerIn: parent
            font.pixelSize: 30
            wrapMode: Text.WordWrap
            color: "white"
            font.bold: true
            smooth: true
        }
    }

    CameraItem {
        id: camera
        anchors.top: parent.top
        anchors.topMargin: parent.height * 0.3
        anchors.horizontalCenter: parent.horizontalCenter
        width: parent.width * 0.7
        height: parent.width * 0.5
        clip: true
        onKwhWaste: {
            messageId.message = "Touch the screen";
            kwhText.text = value + " kWh";
            serverSocket.sendToClient(value);
        }

        onLedOff: {
        }

        onLedOn: {
            if (!ledTimer.running) {
                led.opacity = 1;
                ledTimer.restart();
            }
        }
    }

    Text {
        id: kwhText
        font.pixelSize: 30
        color: "white"
        font.bold: true
        clip: true
        smooth: true
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 10
        anchors.horizontalCenter: camera.horizontalCenter
    }

    ServerSocket {
        id: serverSocket
        onServerLog: {
            //console.log(log);
        }
    }

    Timer {
        id: ledTimer
        interval: 200
        onTriggered: {
            led.opacity = 0;
        }
    }

    Timer {
        id: hide
        interval: 60000
        onTriggered: {
            camera.hideViewFinder();
            camera.opacity = 0;
        }
    }

    Rectangle {
        id: led
        anchors.top: parent.top
        anchors.topMargin: 8
        anchors.right: parent.right
        anchors.rightMargin: 8
        clip: true
        smooth: true
        color: "red"
        width: 25
        height: 25
        radius: 4
        opacity: 0
    }

    Component.onCompleted: {
        serverSocket.start();
        camera.enableCamera(true);
        hide.restart();
        messageId.message = "Starting Camera";
    }

    MouseArea {
        anchors.fill: parent;
        onClicked: {
            camera.showViewFinder();
            camera.opacity = 1;
        }
    }
}
